package com.bfly.mall.shop.controller;

import com.bfly.common.ResponseUtil;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.core.security.ActionModel;
import com.bfly.mall.shop.entity.Shop;
import com.bfly.mall.shop.dto.ShopFormDTO;
import com.bfly.mall.shop.service.IShopService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @author 长沙商脉科技有限公司
 * @date 2020/11/27 19:38
 */
@RestController
@RequestMapping(value = "/manage/shop")
public class ShopController extends BaseController {

    @Autowired
    private IShopService shopService;

    /**
     * 门店信息
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/11/27 19:39
     */
    @GetMapping(value = "/info")
    public void getShop(HttpServletResponse response) {
        Shop shop = shopService.getShop();
        ShopFormDTO dto = new ShopFormDTO();
        if (shop != null) {
            BeanUtils.copyProperties(shop, dto);
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(dto));
    }

    /**
     * 保存门店信息
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/11/27 19:43
     */
    @PostMapping(value = "/merge")
    @ActionModel("保存门店信息")
    public void mergeShop(HttpServletResponse response, @Valid @RequestBody ShopFormDTO formDTO, BindingResult result) {
        validData(result);
        shopService.merge(formDTO);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }
}